package com.szly.phm.admin.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.szly.phm.admin.utils.CustomResponse;
import com.szly.phm.admin.utils.SecurityUtils;
import com.szly.phm.common.entity.dto.FaFault.RadarTypeFaultsNumDTO;
import com.szly.phm.common.entity.dto.screen.*;
import com.szly.phm.common.entity.po.RaServerArea;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.mapper.RaServerAreaMapper;
import com.szly.phm.common.service.IKeyPerformanceService;
import com.szly.phm.common.service.IRaService;
import com.szly.phm.common.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.text.ParseException;

@RestController
@Validated
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/pc/screen")
@Api(tags = "大屏")
@ServletComponentScan(basePackages ="com.szly.phm.admin.config.filter") //filter所在的包，扫描
public class BigScreenController {
    private final IKeyPerformanceService iKeyPerformanceService;

    private final RaServerAreaMapper raServerAreaMapper;
    @ApiOperation(value = "历史数据")
    @GetMapping("/v1/historicalData")
    @CustomResponse
    public RootResponse selectHistoricalData(@Valid KeyPerformancesDTO keyPerformancesDTO) {
        return iKeyPerformanceService.selectKeyPerformances(keyPerformancesDTO);
    }
    @ApiOperation(value = "整机分数历史数据")
    @GetMapping("/v1/radarScoreHistoricalData")
    @CustomResponse
    public RootResponse radarScoreHistoricalData(@Valid ScoreHistoricalDataDTO keyPerformancesDTO) {
        return iKeyPerformanceService.radarScoreHistoricalData(keyPerformancesDTO);
    }
    @ApiOperation(value = "整机分数实时数据")
    @GetMapping("/v1/realTimeRadarScoreData")
    @CustomResponse
    public RootResponse realTimeRadarScoreData(@Valid RealTimeRadarScoreDataDTO keyPerformancesDTO) {
        return iKeyPerformanceService.realTimeRadarScoreData(keyPerformancesDTO);
    }
    @ApiOperation(value = "实时数据")
    @GetMapping("/v1/realTimeFiled")
    @CustomResponse
    public RootResponse selectRealTimeFiled(@Valid RealTimeFiledDTO realTimeFiledDTO) {
        return iKeyPerformanceService.selectRealTimeFiled(realTimeFiledDTO);
    }
    @ApiOperation(value = "固定实时数据")
    @GetMapping("/v1/fixedRealTimeFiled")
    @CustomResponse
    public RootResponse selectFixedRealTimeFiled(@Valid RealTimeFiledDTO realTimeFiledDTO) {
        return iKeyPerformanceService.selectFixedRealTimeFiled(realTimeFiledDTO);
    }
    //0正常1故障
    @ApiOperation(value = "分系统数据")
    @GetMapping("/v1/subsystemData")
    @CustomResponse
    public RootResponse selectSubsystemData(@Valid SubsystemDataDTO realTimeFiledDTO) {
        return iKeyPerformanceService.selectSubsystemData(realTimeFiledDTO);
    }
//    @ApiOperation(value = "系统标定")
//    @GetMapping("/v1/systemCalibration")
//    public RootResponse selectSystemCalibration(@Valid SystemCalibrationDTO realTimeFiledDTO) {
//        return iKeyPerformanceService.selectSystemCalibration(realTimeFiledDTO);
//    }
    @ApiOperation(value = "适配参数变更记录")
    @GetMapping("/v1/adaptationParameters")
    @CustomResponse
    public RootResponse selectAdaptationParameters(@Valid SystemAdaptationParameters realTimeFiledDTO) {
        return iKeyPerformanceService.selectAdaptationParameters(realTimeFiledDTO);
    }
    @ApiOperation(value = "分系统月状态")
    @GetMapping("/v1/subsystemMonthlyStatus")
    @CustomResponse
    public RootResponse selectSubsystemMonthlyStatus(@Valid SubsystemMonthlyStatus realTimeFiledDTO) throws ParseException {
        return iKeyPerformanceService.selectSubsystemMonthlyStatus(realTimeFiledDTO);
    }
    @ApiOperation(value = "查大屏显示分系统")
    @GetMapping("/v1/subsystem")
    @CustomResponse
    public RootResponse selectSubsystem(@NotBlank(message = "radarId不能为空") @RequestParam(value = "radarId") String radarId) {
        return iKeyPerformanceService.selectSubsystem(radarId);
    }
    @ApiOperation(value = "查大屏显示分系统状态")
    @GetMapping("/v1/subsystemStatus")
    @CustomResponse
    public RootResponse selectSubsystemStatus(@NotBlank(message = "radarId不能为空") @RequestParam(value = "radarId") String radarId) {
        return iKeyPerformanceService.selectSubsystemStatus(radarId);
    }

    /**
     * status 0,1,2,3状态良好，弱化，故障，未接入
     * @param radarId
     * @return
     */
    @ApiOperation(value = "查大屏显示固定分系统状态")
    @GetMapping("/v1/fixedSubSystemStatus")
    @CustomResponse
    public RootResponse selectFixedSubsystemFixedStatus(@NotBlank(message = "radarId不能为空") @RequestParam(value = "radarId") String radarId) {
        return iKeyPerformanceService.selectFixedSubsystemFixedStatus(radarId);
    }

    /**
     * 0正常1异常
     * @param radarId
     * @return
     */
    @ApiOperation(value = "查整机允许环境温度湿度")
    @GetMapping("/v1/environmentalParameters")
    @CustomResponse
    public RootResponse selectEnvironmentalParameters(@NotBlank(message = "radarId不能为空") @RequestParam(value = "radarId") String radarId) {
        return iKeyPerformanceService.selectEnvironmentalParameters(radarId);
    }

    /**
     * 查询出当前的分系统里面，保障计划里面，保障类型为月的，今年每个月有没有保障
     * @param radarId
     * @return
     */
    @ApiOperation(value = "业务过程")
    @GetMapping("/v1/businessProcess")
    @CustomResponse
    public RootResponse selectBusinessProcess(@NotBlank(message = "radarId不能为空") @RequestParam(value = "radarId") String radarId,String time) {
        LambdaQueryWrapper<RaServerArea> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RaServerArea::getId, "1");
        RaServerArea area = raServerAreaMapper.selectOne(wrapper);
        if(area==null){
            return RootResponse.error("请先设置当前用户所属区域！！！！");
        }
        return iKeyPerformanceService.selectBusinessProcess(radarId, time, area.getId());
    }
    /**
     *
     * @param radarTypeFaultsNumDTO
     * @return
     */
    @ApiOperation(value = "统计雷达类型故障数量")
    @GetMapping("/v1/radarTypeFaultsNum")
    @CustomResponse
    public RootResponse radarTypeFaultsNum(RadarTypeFaultsNumDTO radarTypeFaultsNumDTO) {
//        var loginUser = SecurityUtils.getLoginUser();
        LambdaQueryWrapper<RaServerArea> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RaServerArea::getId, "1");
        RaServerArea area = raServerAreaMapper.selectOne(wrapper);
        if(area==null){
            return RootResponse.error("请先设置当前用户所属区域！！！！");
        }
        radarTypeFaultsNumDTO.setAreaId(area.getId());
        return iKeyPerformanceService.radarTypeFaultsNum(radarTypeFaultsNumDTO);
    }
    /**
     *
     * @param radarTypeFaultsNumDTO
     * @return
     */
    @ApiOperation(value = "分系统故障数量")
    @GetMapping("/v1/systemFaultsNum")
    @CustomResponse
    public RootResponse systemFaultsNum(RadarTypeFaultsNumDTO radarTypeFaultsNumDTO) {
//        var loginUser = SecurityUtils.getLoginUser();
        LambdaQueryWrapper<RaServerArea> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RaServerArea::getId, "1");
        RaServerArea area = raServerAreaMapper.selectOne(wrapper);
        if(area==null){
            return RootResponse.error("请先设置当前用户所属区域！！！！");
        }
        radarTypeFaultsNumDTO.setAreaId(area.getId());
        return iKeyPerformanceService.systemFaultsNum(radarTypeFaultsNumDTO);
    }
}
